package client;

import java.nio.charset.Charset;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;

public class YaimClientHandler implements IoHandler
{

	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception 
	{
		System.out.println("error in networking...");
		arg1.printStackTrace();
	}

	@Override
	public void messageReceived(IoSession session, Object input) throws Exception
	{
		MessageProcessor.getInstance().process(session, input.toString());		
	}

	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception 
	{		
		//nothing to do
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception
	{		
		//nothing to do
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception
	{				
	    session.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception 
	{		
		//nothing to do
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception 
	{		
		YaimProtocolManager.getInstance().setSession(session);//store the session reference in the ProtocolManager
	}	
}
